package com.woody.aggregate.app.web.settings;

import com.woody.aggregate.ApiLockKeyConst;
import com.woody.aggregate.application.service.settings.ISysCategoryConfigService;
import com.woody.aggregate.settings.api.CategoryConfigApi;
import com.woody.aggregate.settings.req.SaveSysCategoryConfigReq;
import com.woody.aggregate.settings.vo.SysCategoryConfigDetailVo;
import com.woody.aggregate.settings.vo.SysCategoryConfigSaveVo;
import com.woody.base.model.Response;
import com.woody.common.redis.lock.TryLock;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * &copy; All rights Reserved
 *
 * @author wénkǎi.zhāng 2025-03-19
 */
@RestController
@RequestMapping("config/category")
@Api(tags = "分类管理")
public class CategoryConfigController implements CategoryConfigApi {

    @Autowired
    private ISysCategoryConfigService categoryConfigService;

    @Override
    public Response<List<SysCategoryConfigDetailVo>> listCategoryConfig(Integer bizType) {
        List<SysCategoryConfigDetailVo> list = categoryConfigService.listCategoryConfigByType(bizType);
        return Response.success(list);
    }

    @Override
    public Response<SysCategoryConfigSaveVo> saveCategoryConfig(SaveSysCategoryConfigReq request) {
        Integer categoryId = request.getCategoryId();
        if (categoryId != null) {
            categoryConfigService.updateConfig(categoryId, request);
        } else {
            categoryId = categoryConfigService.addConfig(request);
        }
        SysCategoryConfigSaveVo vo = new SysCategoryConfigSaveVo();
        vo.setCategoryId(categoryId);
        return Response.success(vo);
    }

    @Override
    @TryLock(keyNameSpace = ApiLockKeyConst.SYS_CATEGORY_CONFIG_KEY, keys = "#categoryId")
    public Response<Void> deleteCategoryConfig(Integer categoryId) {
        categoryConfigService.deleteById(categoryId);
        return Response.success();
    }
}
